﻿using Microsoft.AspNetCore.Mvc;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class ContentCateController : Base
{
    private readonly LoginUtil _LoginUtil;
    private readonly ContentCateService _contentCateService;

    public ContentCateController(LoginUtil LoginUtil, ContentCateService contentCateService)
    {
        _LoginUtil = LoginUtil;
        _contentCateService = contentCateService;

    }


    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Index)]
    public async Task<IActionResult> CateTreeAsync()
    {
        var allTree = await _contentCateService.GetCateTree(await _LoginUtil.UserInfoAsync());
        return Json(new { status = 1, data = allTree });
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Index)]
    public async Task<IActionResult> IndexAsync()
    {
        var pagedList = await _contentCateService.GetContentCates();
        return Json(new { status = 1, data = pagedList });

    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        var data = await _contentCateService.GetInfo(id);
        IEnumerable<ViewContentCate> cateList = await _contentCateService.GetContentCateListAsync(await _LoginUtil.UserInfoAsync(), ((int)AdminRoleEnum.Role.判断权限));
        return Json(new { status = 1, data, cateList });
    }


    [HttpPost, ActionName("Create")]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "新增内容栏目")]
    public async Task<IActionResult> CreatePostAsync([FromBody] EditContentCate cateModel)
    {
        if (ModelState.IsValid)
        {
            ContentCate cate = new()
            {
                Name = cateModel.Name,
                Pid = cateModel.Pid,
                Theme = cateModel.Theme,
                Ctheme = cateModel.Ctheme,
                Ico = cateModel.Ico,
                Type = cateModel.Type,
                Num = cateModel.Num,
                Sort = cateModel.Sort
            };
            await _contentCateService.InsertAsync(cate);
            return Json(new { status = 1, msg = "新增成功！" });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = error });
        }
    }


    [HttpPut, ActionName("Edit")]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_AddOrEdit)]
    [Log(Name = "修改内容栏目")]
    public async Task<IActionResult> EditPostAsync([FromBody] EditContentCate cateModel, int dyEdit)
    {

        if (ModelState.IsValid)
        {
            ContentCate contentCate = await _contentCateService.GetInfo(cateModel.Id);
            if (dyEdit == 1)
            {

                contentCate.Sort = cateModel.Sort;
                await _contentCateService.UpdateAsync(contentCate);
                return Json(new { status = 1, msg = "修改成功！" });
            }

            contentCate.Pid = cateModel.Pid;
            contentCate.Name = cateModel.Name;
            contentCate.Theme = cateModel.Theme;
            contentCate.Ctheme = cateModel.Ctheme;
            contentCate.Ico = cateModel.Ico;
            contentCate.Sort = cateModel.Sort;
            contentCate.Num = cateModel.Num;
            contentCate.Type = cateModel.Type;
            await _contentCateService.UpdateAsync(contentCate);
            return Json(new { status = 1, msg = " 修改栏目" + contentCate.Name + " id " + cateModel.Id });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = error });
        }
    }

    [HttpDelete, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.ContentCate_Delete)]
    [Log(Name = "删除内容栏目")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _contentCateService.DeleteByPriKeyAsync(id) > 0)
        {
            return Json(new { status = 1, msg = "删除栏目,Id： " + id });
        }
        else
            return Json(new { status = 0, msg = "删除失败！可能因为当前栏目下有内容！" });
    }

}
